package com.am.servlet;

import com.am.service.LoginService;
import com.utils.Meg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class LoginServlet {

    @Autowired
    private LoginService loginService;

    @RequestMapping("/login")
    public String login(){
        return "login";
    }

    @RequestMapping("/loginView")
    public String loginView(){
        return "home";
    }

    @RequestMapping("/loginData")
    @ResponseBody
    public Meg loginData(@RequestParam String username, @RequestParam String password, @RequestParam String authCode, HttpSession httpSession){
        return loginService.login(username,password,authCode,httpSession);
    }

    @RequestMapping("/loginJson")
    @ResponseBody
    public Meg loginJson(HttpSession httpSession){
        return loginService.homeJson(httpSession);
    }

    @RequestMapping("/exit")
    public String exit(HttpSession httpSession){
        httpSession.removeAttribute("user");
        return "login";
    }
}
